package com.skt.common_hr.training.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.approval.IRequestApprovalProxy;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.training.model.vo.TrainingDetailVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	public class TrainingApprovalProxy extends CommonProxy implements IRequestApprovalProxy
	{
		public static const NAME:String = "TrainingApprovalProxy";
		public var dataProvider:TrainingDetailVO;
		
		private var templateHTML:String = "";
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function TrainingApprovalProxy(data:Object=null)
		{
			super(NAME, data);
			loadTemplate();
		}
		
		private var _key:String = "";
		public function get key():String
		{
			return _key;
		}
		public function set key(k:String):void
		{
			_key = k;
		}
		
		public function get title():String 
		{
			var tmp:String = "교육 품의 (";
			if(DateUtil.daySpan(dataProvider.BEGDA, dataProvider.ENDDA) > 1 ) {
				tmp += DateUtil.toDefaultString(dataProvider.BEGDA) + " ~ " + DateUtil.toDefaultString(dataProvider.ENDDA) + ")";
			}
			else {
				tmp += DateUtil.toDefaultString(dataProvider.BEGDA) + ")";
			}
			return tmp;
		}
		
		public function get content():String
		{
			return buildHTML(dataProvider);
		}
		
		public function get appCode():String 
		{
			return "07"; 	//
		}
 		
 		private function loadTemplate():void
		{
			var request:URLRequest = new URLRequest("assets/template/training.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}
 			
		private function templateResult(event:Event):void
		{
			templateHTML = loader.data;
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
		}



		private function buildHTML(vo:TrainingDetailVO):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";
			
			var ptnCname:RegExp  = /__CNAME__/; 
			var ptnIpernr:RegExp = /__IPERNR__/; 
			var ptnLstext:RegExp = /__LSTEXT__/;
			var ptnDstext:RegExp = /__DSTEXT__/;
			var ptnTxtsing:RegExp= /__TXTSING__/;
			var ptnUstext:RegExp = /__USTEXT__/;
			var ptnRstext:RegExp = /__RSTEXT__/;
			var ptnIkostt:RegExp = /__IKOSTT__/;
			var ptnNhours:RegExp = /__NHOURS__/;
			var ptnDate:RegExp 	 = /__DATE__/;
			var ptnNdays:RegExp  = /__NDAYS__/;
			
			htmlAll = htmlAll.replace(ptnCname, Variables.myName);
			htmlAll = htmlAll.replace(ptnIpernr, Variables.myPernr);
			htmlAll = htmlAll.replace(ptnLstext, StringUtil.NVL(vo.L_STEXT));
			htmlAll = htmlAll.replace(ptnDstext, StringUtil.NVL(vo.D_STEXT));
			htmlAll = htmlAll.replace(ptnTxtsing, StringUtil.NVL(vo.TXTSING));
			htmlAll = htmlAll.replace(ptnUstext, StringUtil.NVL(vo.U_STEXT));
			htmlAll = htmlAll.replace(ptnRstext, StringUtil.NVL(vo.R_STEXT));
			htmlAll = htmlAll.replace(ptnIkostt, StringUtil.NVL(vo.IKOSTT));
			htmlAll = htmlAll.replace(ptnNhours, StringUtil.NVL(vo.NHOURS));
			htmlAll = htmlAll.replace(ptnDate, DateUtil.toDefaultString(vo.BEGDA) + ' ~ ' + DateUtil.toDefaultString(vo.ENDDA));
			htmlAll = htmlAll.replace(ptnNdays, StringUtil.NVL(vo.NDAYS));
			
			return htmlAll;
		}
		

	}
}